<?php
namespace Admin\Controller;

use Util\AccessController;

/*通知*/
require_once(THINK_PATH . "Library/Util/WxSendMsg/SendMsg.php");

class IndexController extends AccessController {
    public function index() {
//        dump(GetBookToIsbnInDDAPI('9781409310815'));

        //套装书RED
        $TzBook_red = $this->TZBOOK_RED();
        //所有书籍RED
        $this->S('ALL_BOOK_RED', $TzBook_red);

        //购书订单RED
        $Order_red1 = $this->Buy_Order_RED();
        //回收订单RED
        $Order_red2 = $this->HS_Order_RED();
        //所有订单RED
        $this->S('ALL_ORDER_RED', $Order_red1 + $Order_red2);
        $this->display();
    }

    public function test() {

        $this->display();
    }

    public function AddOrder_SendMsg() {
        $order_id = $_GET['order_id'];
        $LS = Service('M', 'order');
        $where['order_id'] = $order_id;
        $Order = $LS->find($where);
        $APPID = C('WX_APPID'); //微信公共号appid
        $SECRETID = C('WX_SECRET');//微信公共号 SECRETID
        $template_id = 'lXm-BetKbFLVkjeekr0ulWSKoxx1WJcl-yj2ZuHsENs';//消息模板id
        $user = C('ADMIN_OPENID');
        //消息体信息
        $first = '客户下单买书啦，快去帮TA发快递';
        $keyword1 = '共' . $Order['order_num'] . '本书';
        $keyword2 = $Order['order_book_money'];
        $keyword3 = $Order['order_address_name'] . ' ' . $Order['order_phone'];
        $keyword4 = $Order['order_address'];
        $keyword5 = $Order['order_start_time'];
        $remark = '-->点击查看详情吧o(∩_∩)o ';
        $url = 'http://www.yanxiang.pw/orders/info?order_id=' . $order_id;//链接
        $Send = new \Util\WxSendMsg\SendMsg();
        $data = array($keyword1,
            $keyword2,
            $keyword3,
            $keyword4,
            $keyword5);
        foreach ($user as $k => $v) {
            $Send->init($APPID, $SECRETID, $v, $template_id, $url);
            $Send->Setfirst($first);
            $Send->Setremark($remark);
            $Send->Setdata($data);
            $OK = $Send->SendMsg();
        }
    }

    //套装书RED
    public function TZBOOK_RED() {
        $LS = Service('M', 'tz_book_list');
        $data = $LS->query();
        $LS1 = Service('M', 'tz_book');
        $red = 0;
        foreach ($data as $k => $v) {
            $notOKnum = 10000;
            $where = array('ts_tz_book.TZ_BOOK_LIST_ID' => $v['ID']);
            $join = 'left join ts_book on ts_book.ID = ts_tz_book.BOOK_ID';
            $files = 'ts_book.*';
            $bookdata = $LS1->join($where, $join, $files, null, null);
            foreach ($bookdata as $k1 => $v1) {
                if ($v1['STOCK']) {
                    if ($v1['STOCK'] < $notOKnum) {
                        $notOKnum = $v1['STOCK'];
                    }
                } else {
                    $notOKnum = 0;
                }

            }
            if ($notOKnum < 10000 && $notOKnum > 0) {
                $red++;
            }
        }
        $this->S('TZBOOK_RED', (int)$red);
        return $red;
    }

    //购书订单RED
    public function Buy_Order_RED() {
        $LS = Service('M', 'order');
        $where['order_type'] = 2;
        $where['order_leixing'] = 2;
        $red = $LS->count($where);
        $this->S('BUY_ORDER_RED', (int)$red);
        return $red;
    }

    //回收订单RED
    public function HS_Order_RED() {
        $LS = Service('M', 'order');
        $where['order_type'] = 1;
        $where['order_leixing'] = 3;
        $red = $LS->count($where);
        $this->S('HS_ORDER_RED', (int)$red);
        return $red;
    }
}